    <include file="public@header" />
    <style>.expander{margin-left: -20px;}</style>
    <style type="text/css">
    .form-group{
        width: 700px;
    }
    .icon{
      padding: 5px;
      cursor: pointer;
      display: none;
  }
  tbody tr:hover .icon{
      display: inline-block;
  }
  .role{
      cursor: pointer;
  }
</style>
</head>
<body>
 <div class="wrap js-check-wrap">
    <div>
        <h4>{$company.company_name} 角色管理</h4>
    </div>
        <ul class="nav nav-tabs">
           <li class="active"><a href="javascript:;">角色树状图</a></li>
       </ul> 
       <div id="role_treeview"></div>
</div>

   <!--权限管理菜单弹窗-->
   <div id="qxgl_tc" style="display: none;">
       <div id="qxgl_treeview" style="margin: 20px;"></div>
   </div>
   <!--添加下级角色弹窗-->
   <div id="addgw_tc"  style="display: none;">
        <form id="addgw_form" class="form-horizontal margin-top-20" method="post" action="{:url('rbac/roleAddPost')}">
            <div class="form-group">
                <label for="inputEmail3" class="col-sm-2 control-label"><span class="form-required">*</span>角色名称</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" name="role_name"  placeholder="填写角色名称" required>
                </div>
            </div>
            <div class="form-group">
                <label for="inputPassword3" class="col-sm-2 control-label"><span class="form-required">*</span>上级名称</label>
                <div class="col-sm-10">
                    <select name="role" class="form-control">
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label for="inputPassword3" class="col-sm-2 control-label"><span class="form-required">*</span>所属部门</label>
                <div class="col-sm-10">
                    <select name="department" class="form-control">
                    </select>
                </div>
            </div>
            <div class="form-group">
                <label for="inputPassword3" class="col-sm-2 control-label">岗位备注</label>
                <div class="col-sm-10">
                    <script type="text/plain" id="content"  name="remark"></script>
                </div>
            </div>
        </form>
    </div>
    <!--添加人员弹窗-->
    <div id="adduser_tc"  style="display: none;">
         <form id="adduser_form" class="form-horizontal margin-top-20" method="post" action="">
            <div class="form-group">
                <label for="inputEmail3" class="col-sm-2 control-label"><span class="form-required">*</span>姓名</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" name="user_login"  placeholder="填写姓名" required>
                </div>
            </div>
            <div class="form-group">
                <label for="inputEmail3" class="col-sm-2 control-label"><span class="form-required">*</span>邮箱</label>
                <div class="col-sm-10">
                    <input type="email" class="form-control" name="user_email"  placeholder="填写邮箱" required>
                </div>
            </div>
            <div class="form-group">
                <label for="inputEmail3" class="col-sm-2 control-label"><span class="form-required">*</span>密码</label>
                <div class="col-sm-10">
                    <input type="password" class="form-control" name="user_pass"  placeholder="填写密码" required>
                </div>
            </div>
            <div class="form-group">
                <label for="inputEmail3" class="col-sm-2 control-label"><span class="form-required">*</span>确认密码</label>
                <div class="col-sm-10">
                    <input type="password" class="form-control" name="d_pass"  placeholder="填写确认密码" required>
                </div>
            </div>
            <div class="form-group">
                <label for="inputEmail3" class="col-sm-2 control-label"><span class="form-required">*</span>角色类型</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" name="role_name"  placeholder="填写角色名称" readonly>
                </div>
            </div>
        </form>
    </div>

    <div id="showUser_tc" style="display: none;">
        <div style="padding: 10px 15px;">
            <table class="table table-bordered table-hover">
                <thead>
                    <tr>
                        <th>序号</th>
                        <th>员工编号</th>
                        <th>姓名</th>
                        <th>电话</th>
                        <th>邮箱</th>
                        <th>上次登入时间</th>
                        <th>状态</th>
                    </tr>
                </thead>
                <tbody>
                </tbody>
            </table>
        </div>
    </div>
<script src="__STATIC__/js/admin.js"></script>
<script src="__STATIC__/js/layer/layer.js"></script>
<script src="__STATIC__/js/zy-treeview/zy-treeview.1.0.js"></script>
<script type="text/javascript" src="__STATIC__/js/ueditor/ueditor.config.js"></script>
<script type="text/javascript" src="__STATIC__/js/ueditor/ueditor.all.min.js"></script>
<script type="text/javascript">
    $(function () {
        editorcontent = new baidu.editor.ui.Editor();
        editorcontent.render('content');
        try {
            editorcontent.sync();
        } catch (err) {
        }
        $('.btn-cancel-thumbnail').click(function () {
            $('#thumbnail-preview').attr('src', '__TMPL__/public/assets/images/default-thumbnail.png');
            $('#thumbnail').val('');
        });

    });
</script>

<script type="text/javascript">
    (function($){
        var data={$list};
        $('#role_treeview').treeView({
            debug:true,
            data:data,
            //获取子级回调方法
            ajaxGetChildList:function(x){
                var url="{:url('rbac/ajax_get_child')}";
                var returnData=null;
                var layload =null;
                //需要返回值，不能使用异步方法发起请求
                $.ajax({url:url,data:{parent_id:x,level:$('#nodeid_'+x).data('level')},async:false,
                    beforeSend:function(){ //加载层
                        layload = layer.load(0, {shade: [0.1,'#fff']}); 
                    },
                    success:function(data){
                        layer.close(layload);//回调成功关闭加载层
                        returnData=data;
                    },
                    error:function(){
                        layer.close(layload);//回调关闭加载层
                        layer.alert('请求异常，请稍后再试！');
                    }
                });
                return returnData;
            }
        });
    })(jQuery);
</script>

<script>
    (function($){
            //绑定事件  功能选择
            //添加岗位
            $('#role_treeview').on('click','.addgw',function(){
                //获取当前角色的id
                var id=$(this).parent().parent().data('tag');
                $('#addgw_form').find('input[name=role_name]').val('');
                //获取信息角色信息
                $.post("{:url('rbac/getRoleInfo')}",{id:id},function(data){
                    $('#addgw_tc').find('select[name=department]').html(data.department);
                    $('#addgw_tc').find('select[name=role]').html(data.role);
                    $('#addgw_tc').find('select[name=role] option').each(function(){
                        if($(this).val()==id){
                            $(this).attr('selected',true);
                            $('#addgw_tc').find('select[name=department]').find('option[value='+$(this).data('dep')+']').attr('selected',true);
                        }
                    });
                    $('#addgw_tc').find('select[name=role]').on('change',function(){
                            var dep=$(this).find('option:selected').data('dep');
                            $('#addgw_tc').find('select[name=department]').find('option').removeAttr('selected');
                            $('#addgw_tc').find('select[name=department]').find('option[value='+dep+']').prop('selected',true);
                    });
                });
                var index=layer.open({
                    type: 1,
                    title: '添加下级岗位',
                    area: ['700px', '600px'],
                    closeBtn: 1,
                    btn:['保存','取消'],
                    shadeClose: true,
                    skin: 'yourclass',
                    scrollbar: false,
                    content: $("#addgw_tc "),
                    yes:function(){
                        if($.trim($('#addgw_form').find('input[name=role_name]').val()).length==0){
                            layer.msg("请填写角色名称！");
                            $('#addgw_form').find('input[name=role_name]').focus();
                        }else{
                            //$('#addgw_form').submit();
                            var data={};
                            $.each($("#addgw_form").serializeArray(),function(key,value){
                                data[value.name]=value.value;
                            });
                            $.post("{:url('rbac/roleAddPost')}",data,function(data){
                                layer.msg(data.message);
                                if(data.status=='success'){
                                    setTimeout(function(){
                                                window.location.reload();
                                    },1200);
                                }
                            });
                        }
                    }
                });
            });
            //编辑岗位
            $('#role_treeview').on('click','.editgw',function(){
                //获取当前要编辑的角色的id
                var id=$(this).parent().parent().data('tag');
                //获取信息角色信息
                $.post("{:url('rbac/getRoleInfo')}",{id:id,tag:'edit'},function(data){
                    $('#addgw_tc').find('select[name=department]').html(data.department);
                    $('#addgw_tc').find('select[name=role]').html(data.role);
                    $('#addgw_tc').find('select[name=role] option').each(function(){
                        if($(this).val()==data.roleInfo.parent_id){
                            $(this).attr('selected',true);
                            $('#addgw_tc').find('select[name=department]').find('option[value='+$(this).data('dep')+']').attr('selected',true);
                        }
                    });
                    $('#addgw_tc').find('select[name=role]').on('change',function(){
                            var dep=$(this).find('option:selected').data('dep');
                            $('#addgw_tc').find('select[name=department]').find('option').removeAttr('selected');
                            $('#addgw_tc').find('select[name=department]').find('option[value='+dep+']').prop('selected',true);
                    });
                    $('#addgw_form').find('input[name=role_name]').val(data.roleInfo.name);

                    $('#addgw_tc').find('select[name=department] option').each(function(){
                        if($(this).val()==data.roleInfo.department_id){
                            $(this).attr('selected',true);
                        }else{
                            $(this).removeAttr('selected');
                        }
                    });


                    var str = data.roleInfo.remark;
                    setTimeout(function(){
                        UE.getEditor("content").ready(function(){
                            this.setContent(str);
                        });   
                    },666);
                    var index=layer.open({
                            type: 1,
                            title: '编辑岗位',
                            area: ['700px', '600px'],
                            closeBtn: 1,
                            btn:['保存','取消'],
                            shadeClose: true,
                            skin: 'yourclass',
                            scrollbar: false,
                            content: $("#addgw_tc "),
                            yes:function(){
                                if($.trim($('#addgw_form').find('input[name=role_name]').val()).length==0){
                                    layer.msg("请填写角色名称！");
                                    $('#addgw_form').find('input[name=role_name]').focus();
                                }else{
                                    //$('#addgw_form').submit();
                                    var data={};
                                    $.each($("#addgw_form").serializeArray(),function(key,value){
                                        data[value.name]=value.value;
                                    });
                                    data['role_id']=id;
                                    $.post("{:url('rbac/roleEditPost')}",data,function(data){
                                        layer.msg(data.message);
                                        if(data.status=='success'){
                                            setTimeout(function(){
                                                window.location.reload();
                                            },1200);
                                        }
                                    });
                                }
                            }
                    });
                });
            });
            //添加人员
            $('#role_treeview').on('click','.adduser',function(){
                //获取当前要编辑的角色的id
                var id=$(this).parent().parent().data('tag');
                $("#adduser_tc").find('input[name=role_name]').val($(this).parent().parent().find('.nodeText').text());
                 var index=layer.open({
                            type: 1,
                            title: '添加人员',
                            area: ['700px', '450px'],
                            closeBtn: 1,
                            btn:['添加','取消'],
                            shadeClose: true,
                            skin: 'yourclass',
                            scrollbar: false,
                            content: $("#adduser_tc "),
                            yes:function(){
                                //非空验证
                                var isOk=true;
                                $.each($('#adduser_tc').find('input'),function(){
                                    if($.trim($(this).val()).length==0){
                                        $(this).focus();
                                        isOk=false;
                                        return false;
                                    }
                                });
                                if(isOk){
                                    //提交信息
                                    var data={};
                                    $.each($("#adduser_form").serializeArray(),function(key,value){
                                        if(value.name!="role_name")
                                        data[value.name]=value.value;
                                    });
                                    data['role_id']=id;
                                    $.post("{:url('rbac/ajaxAddUser')}",data,function(data){
                                        layer.msg(data.message);
                                        if(data.status=='success'){
                                            setTimeout(function(){
                                                window.location.reload();
                                            },1200);
                                        }
                                    });
                                }else{
                                    layer.msg('请将信息填写完整在提交！');
                                }
                            }
                        });
            });
            //权限管理
            $('#role_treeview').on('click','.qxgl',function(){
                //获取当前角色的id
                var id=$(this).parent().parent().data('tag');
                console.log(id);
                //后台获取授权菜单列表数据
                var datas=null;
                //同步获取
                $.ajax({url:"{:url('rbac/ajaxGetRoleMenu')}",data:{id:id},
                    beforeSend:function(){ //加载层
                     layload = layer.load(0, {shade: [0.1,'#fff']}); 
                 },
                 success:function(data){
                    console.log(data);
                        layer.close(layload);//回调成功关闭加载层
                        loadtree(data,id);
                    },
                    error:function(){
                        layer.close(layload);//回调关闭加载层
                        layer.alert('请求异常，请稍后再试！');
                    }
                });

            });
            function loadtree(datas,id){
                if(datas.length==0){
                    $("#qxgl_treeview").parent().text('未获得相应的授权数据，请联系管理员！');
                }
                            //调用插件显示菜单
                            $("#qxgl_treeview").treeView({data:datas,checkbox:true, deep:1,fontColor:'#0D8FED'});
                            var index=layer.open({
                                type: 1,
                                title: '角色权限管理',
                                area: ['700px','90%'],
                                closeBtn: 1,
                                btn:['保存','取消'],
                                shadeClose: true,
                                skin: 'yourclass',
                                content: $("#qxgl_tc "),
                                yes:function(){
                        //保存按钮回调
                    //获取选中的数据
                    var cbxList=$('#qxgl_treeview').find('.tree_cbx');
                    var list=[];
                    $.each(cbxList,function(){
                        if($(this).is(':checked')){
                            var i=$(this).parent().parent().data('tag');
                            list.push(i);
                        }
                    });
                        //提交到后台
                        var url="{:url('rbac/ajax_role_menu_add')}";
                        $.ajax({url:url,data:{id:id,list:list},type:'post',dataType:'json',success:function(data){
                            if(data.status=='success'){
                                layer.close(index);
                            }
                            layer.msg(data.message);
                        }});
                    },
                });
                        }
            //删除角色
            $('#role_treeview').on('click','.deletejs',function(){ 
                //获取当前角色的id
                var id=$(this).parent().parent().data('tag');
                layer.confirm('确定要删除此角色信息吗？', {
                        title:'删除提示',
                        btn: ['确定','取消'] //按钮
                    }, function(){
                        $.ajax({url:"{:url('rbac/roleDelete')}",data:{id:id},
                            beforeSend:function(){ //加载层
                                layload = layer.load(0, {shade: [0.1,'#fff']}); 
                            },
                            success:function(data){
                                layer.close(layload);//回调成功关闭加载层
                                layer.msg(data.message);
                                if(data.status=="success"){
                                    setTimeout(function(){
                                        window.location.reload();
                                    },1500);
                                }
                            },
                            error:function(){
                                layer.close(layload);//回调关闭加载层
                                layer.msg('请求异常，请稍后再试！');
                            }
                        });
                    }, function(){
                        layer.msg('操作已取消！');
                });
            });

            //查看员工
            $('#role_treeview').on('click','.badge',function(){
                var id=$(this).parent().parent().data('tag');
                                 var index=layer.open({
                    type: 1,
                    title: '查看人员',
                    area: ['700px', '450px'],
                    closeBtn: 1,
                    btn:['关闭'],
                    shadeClose: true,
                    skin: 'yourclass',
                    scrollbar: false,
                    content: $("#showUser_tc ")
                });
                $('#showUser_tc table tbody').html('');
                //获取人员
                $.get("{:url('rbac/getUserInfo')}?id="+id,function(data){
                    data=JSON.parse(data);
                    if(data.status!="success"){
                        layer.msg(data.message);
                    }else{
                        console.log(data);
                        //填充数据
                        $('#showUser_tc table tbody').append(data.data);
                    }
                });
            });
        })(jQuery);
    </script>
</body>
</html>